package org.snail.chat.service.chatchain.chain;

import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;
import org.snail.chat.service.chatchain.dao.ChatCreateOrUpdateDAO;
import org.snail.constant.ErrCodeConstant;
import org.snail.dept.bean.TreeNode;
import org.snail.exception.CommonException;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 获取当前Level人员到群组
 */
@Log4j
public class CurrentLevelChatCreateOrUpdateHandler extends ChatCreateOrUpdateHandler {

    private final Logger LOGGER = Logger.getLogger(CurrentLevelChatCreateOrUpdateHandler.class);

    @Override
    public void doCreateOrUpdate(TreeNode treeNode, Map<String, ChatCreateOrUpdateDAO> chatCreateOrUpdateDAOMap) throws Exception {

        if (null == treeNode || StringUtils.isEmpty(treeNode.getId())) {
            LOGGER.error("treeNode中缺乏重要参数！treeNode :" + treeNode);
            throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "treeNode中缺乏重要参数！treeNode :" + treeNode);
        }

        //添加当前基本人员到群组内
        List<String> userIdList = treeNode.getUserList();
        if (null != userIdList && !userIdList.isEmpty()) {
            //获取当前树的ChatCreateOrUpdateDAO
            ChatCreateOrUpdateDAO chatCreateOrUpdateDAO = getChatCreateOrUpdateDAO(treeNode, chatCreateOrUpdateDAOMap);

            log.info(chatCreateOrUpdateDAO.getName() + " userIdList : " + userIdList);
            //添加数据库中的人员列表到需要添加的队列中
            addUserChatCreateOrUpdateDAO(chatCreateOrUpdateDAO, userIdList);

            //重新放回到Map中
            chatCreateOrUpdateDAOMap.put(treeNode.getId(), chatCreateOrUpdateDAO);
        }
    }
}
